<?php

namespace App\Admin\Http\Controllers\System;

use Flash;
use App\Admin\Eloquent\Role;
use App\Admin\Eloquent\Menu;
use App\Admin\Eloquent\RoleMenu;
use App\Admin\Http\Controllers\Controller;
use App\Extensions\Pagination\BootstrapThreePresenter;
use \Illuminate\Http\Request;

class MenusController extends Controller
{
	/**
	 * 查询菜单信息
	 * 
	 * @param  mixed $typeId
	 * @return \Illuminate\Http\Response
	 */
	public function index(Request $request)
	{
		$query = $request->only('search');

		$list = Menu::paging_query($query);

	    if (!empty($query)) {
	      $list->appends($query);
	    }

		$links = (new BootstrapThreePresenter($list))->render();

		return view('admin.system.menus.index', compact('list', 'links', 'query'));
	}

	/**
	 * 新增菜单息表单
	 * 
	 * @return \Illuminate\Http\Response
	 */
	public function create()
	{
		$menu=new Menu();
		$parents=$menu->getMenusByLevel(1);

		return view('admin.system.menus.create',compact("parents"));
	}

	/**
     * 新增菜单信息
     * 
     * @param  \App\Admin\Http\Requests\CodeRequest $request
     * @return \Illuminate\Http\Response
     *
     * @throws \App\Exceptions\BusinessException
     */
	public function store(Request $request)
	{	
		$data=$request->input();
		$pids=$data['pid'];
		rsort($pids);
		foreach ($pids as $value) {
			if(!empty($value)){
				$pid=$value;
				break;
			}
		}
		if(empty($pid)){
			$data['pid']=0;
		}else{
			$data['pid']=$pid;
		}
		$menu = new Menu($data);

		$menu->save();

		RoleMenu::insert(array('role_id'=>1,'menu_id'=>$menu->id));

		Flash::success('新增菜单信息成功');

		return $this->redirectAction('System\MenusController@show', [$menu]);
	} 

	/**
	 * 查看菜单信息
	 * 
	 * @return \Illuminate\Http\Response
	 */
	public function show($id)
	{
		$menu=Menu::findOrFail($id);
		$parents=$menu->getParents($menu['pid']);

		return view('admin.system.menus.show', compact('menu','parents'));
	}
	/**
	 * 编辑菜单信息表单
	 * 
	 * @param  mixed $typeId
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id)
	{
		$menu=Menu::findOrFail($id);
		$parents=$menu->getParents($menu['pid']);

		return view('admin.system.menus.edit', compact('menu','parents'));
	}

	/**
     * 编辑菜单信息
     * 
     * @param  mixed  $id
     * @param  \App\Admin\Http\Requests\CodeRequest $request
     * @return \Illuminate\Http\Response
     *
     * @throws \App\Exceptions\BusinessException
     */
	public function update($id, Request $request)
	{

		$menu = Menu::findOrFail($id);

		$menu->update($request->input());

		Flash::success('编辑菜单信息成功');

		return $this->redirectAction('System\MenusController@show', [$menu]);
	}

	/**
	 * 删除菜单
	 *
	 * @param  mixed $codeId
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id)
	{

	    $menu = Menu::findOrFail($id);

	    $menu->delete();

	    Flash::success('删除菜单成功');

	    return $this->redirectAction('System\MenusController@index');
	}


	/**
	 * 获取子级菜单
	 * @param  Request $request [description]
	 */
	public function getChildren(Request $request){
        $id = $request->input('id');
        $menu=new Menu();
        $list=$menu->getMenusByPid($id);
        return json_encode($list);
	}

}
